/**
 * 
 * @Author TOUR U6 WS 2013 PM&T 
 * @Created 04.12.2013
 * 
 */
package de.hhn.pmt.tour.u6.gui.ticketkauf;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.orm.PersistentException;

import de.hhn.pmt.tour.u6.RegistrierterBenutzer;
import de.hhn.pmt.tour.u6.RegistrierterBenutzerDAO;
import de.hhn.pmt.tour.u6.gui.benutzer.BenutzerAnmeldenFrame;

/**
 * The Class Ticketkauf.
 * 
 * Alexander Neuberger & Maxim Schleicher
 */
public class Ticketkauf extends JDialog {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1606896484782412553L;

	/** The content pane. */
	private JPanel contentPane;

	/** The txt name. */
	private JTextField txtName;

	/** The txt surname. */
	private JTextField txtSurname;

	/** The txt username. */
	private JTextField txtUsername;

	/** The txt email. */
	private JTextField txtEmail;

	/** The txt ticketnumber. */
	private JTextField txtTicketnumber;

	/** The ticket benutzer. */
	private RegistrierterBenutzer ticketBenutzer;

	/**
	 * Sets the ticket benutzer.
	 * 
	 * @param benutzer
	 *            the new ticket benutzer
	 */
	private void setTicketBenutzer(RegistrierterBenutzer benutzer) {
		ticketBenutzer = benutzer;
	}

	/**
	 * Fill frame.
	 */
	private void fillFrame() {
		setTicketBenutzer(BenutzerAnmeldenFrame.benutzer);
		// TODO Auto-generated method stub
		RegistrierterBenutzer biggestTicketnumber = new RegistrierterBenutzer();
		try {
			biggestTicketnumber.setTicketnummer(0);
			RegistrierterBenutzer[] reg = RegistrierterBenutzerDAO
					.listRegistrierterBenutzerByQuery(null, "ticketnummer");
			for (RegistrierterBenutzer benutzer : reg) {
				if (benutzer.getTicketnummer() > biggestTicketnumber
						.getTicketnummer()) {
					biggestTicketnumber.setTicketnummer(benutzer
							.getTicketnummer());
				}
			}
		} catch (PersistentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BenutzerAnmeldenFrame.benutzer.setTicketnummer(biggestTicketnumber
				.getTicketnummer() + 1);

		txtName.setText(ticketBenutzer.getNachname());
		txtSurname.setText(ticketBenutzer.getVorname());
		txtUsername.setText(ticketBenutzer.getBenutzername());
		txtEmail.setText(ticketBenutzer.getEmail());
		txtTicketnumber.setText("" + ticketBenutzer.getTicketnummer());

		try {
			RegistrierterBenutzerDAO.save(ticketBenutzer);
		} catch (PersistentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame.
	 */
	public Ticketkauf() {
		setResizable(false);
		this.setModal(true);
		this.setLocationRelativeTo(null);
		setTitle("LNdM -Tour U6 - Ticket kaufen");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblWillkommen = new JLabel(
				"<HTML><U>Willkommen beim Ticketkauf!<U><HTML>");
		lblWillkommen.setBounds(10, 10, 200, 14);
		contentPane.add(lblWillkommen);

		JLabel lblName = new JLabel("Name: ");
		lblName.setBounds(10, 40, 100, 14);
		contentPane.add(lblName);

		JLabel lblSurname = new JLabel("Nachname: ");
		lblSurname.setBounds(10, 60, 100, 14);
		contentPane.add(lblSurname);

		JLabel lblUsername = new JLabel("Benutzername: ");
		lblUsername.setBounds(10, 80, 100, 14);
		contentPane.add(lblUsername);

		JLabel lblEmail = new JLabel("E-Mailadresse: ");
		lblEmail.setBounds(10, 100, 100, 14);
		contentPane.add(lblEmail);

		JLabel lblTicketnumber = new JLabel("Ticketnummer:  ");
		lblTicketnumber.setBounds(10, 120, 100, 14);
		contentPane.add(lblTicketnumber);

		txtName = new JTextField();
		txtName.setColumns(10);
		txtName.setBounds(120, 37, 150, 20);
		txtName.setEditable(false);
		contentPane.add(txtName);

		txtSurname = new JTextField();
		txtSurname.setColumns(10);
		txtSurname.setBounds(120, 57, 150, 20);
		txtSurname.setEditable(false);
		contentPane.add(txtSurname);

		txtUsername = new JTextField();
		txtUsername.setColumns(10);
		txtUsername.setBounds(120, 77, 150, 20);
		txtUsername.setEditable(false);
		contentPane.add(txtUsername);

		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(120, 97, 150, 20);
		txtEmail.setEditable(false);
		contentPane.add(txtEmail);

		txtTicketnumber = new JTextField();
		txtTicketnumber.setColumns(10);
		txtTicketnumber.setBounds(120, 117, 150, 20);
		txtTicketnumber.setEditable(false);
		contentPane.add(txtTicketnumber);

		JButton btnShowTicket = new JButton("Ticket anzeigen");
		btnShowTicket.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnShowTicket.setBounds(10, 227, 131, 23);
		contentPane.add(btnShowTicket);

		JButton btnSaveTicket = new JButton("Download Ticket");
		btnSaveTicket.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnSaveTicket.setBounds(151, 227, 137, 23);
		contentPane.add(btnSaveTicket);

		JButton btnValidate = new JButton("OK");
		btnValidate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnValidate.setBounds(335, 227, 89, 23);
		contentPane.add(btnValidate);

		JLabel lblQRCode = new JLabel("QR Code:");
		lblQRCode.setBounds(324, 40, 100, 14);
		contentPane.add(lblQRCode);

		JLabel lblQRCodeImage = new JLabel("QR Code Image");
		lblQRCodeImage.setBounds(324, 60, 80, 80);
		contentPane.add(lblQRCodeImage);

		if (BenutzerAnmeldenFrame.benutzer.getTicketnummer() == 0) {
			fillFrame();
		}
		this.setVisible(true);

	}
}
